local bowangzuo = fk.CreateSkill({
    name = "bowangzuo",
})

Fk:loadTranslationTable{
    ["bowangzuo"] = "王佐",
    [":bowangzuo"] = "每当你受到1点伤害后，你可以令一名角色将手牌补至其体力上限张（若已达上限则摸2张），或弃至与你当前体力值相同张数。",
    ["#bowangzuo-choose"] = "王佐：选择一名角色",
    ["#bowangzuo-effect"] = "王佐：选择补牌至体力上限或弃牌至当前体力值",
    ["#bowangzuo-discard"] = "王佐：请弃置至与目标体力值相同张数",
    ["$bowangzuo1"] = "臣未寒在胸之血，君何吝雨露雷霆？",
    ["$bowangzuo2"] = "龙困浅滩，尚需云雨为济，况天子乎？",
}

bowangzuo:addEffect(fk.Damaged, {
    anim_type = "masochism",
    trigger_times = function(self, event, target, player, data)
        return data.damage  -- 按伤害点数触发对应次数
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        -- 选择目标角色（支持取消）
        local to = room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 1,
            targets = room.alive_players,
            skill_name = bowangzuo.name,
            prompt = "#bowangzuo-choose",
            cancelable = true,
        })
        if #to == 0 then return false end
        
        -- 选择效果（补牌/弃牌）
        local choices = {"补牌至体力上限", "弃牌至当前体力值"}
        local choice = room:askToChoice(player, {
            choices = choices,
            skill_name = bowangzuo.name,
            prompt = "#bowangzuo-effect",
        })
        event:setCostData(self, {tos = to[1], choice = choice})
        return true
    end,
    on_use = function(self, event, target, player, data)
        local to = event:getCostData(self).tos
        local choice = event:getCostData(self).choice
        local myHp = player.hp
        local room = player.room

        if to.dead then return end
        
        -- 打印调试信息（确认逻辑走向）
        print("[王佐技能] 选择效果：", choice)
        print("[王佐技能] 目标玩家ID：", to.id, "当前体力：", to.hp, "体力上限：", to.maxHp)
        print("[王佐技能] 发动者体力：", myHp)

        -- 补牌至体力上限（严格隔离逻辑）
        if choice == "补牌至体力上限" then
            local currentHand = to:getHandcardNum()
            local maxHand = to.maxHp
            
            -- 防护：体力上限必须大于0
            if maxHand <= 0 then
                room:sendLog{type = "#WangzuoInvalidMaxHp", from = player.id, arg = to.id}
                return
            end
            
            print("[补牌逻辑] 当前手牌数：", currentHand, "目标上限：", maxHand)
            if currentHand >= maxHand then
                to:drawCards(2, bowangzuo.name)
                room:sendLog{type = "#WangzuoDraw2Cards", from = player.id, arg = to.id}
            else
                local needDraw = maxHand - currentHand
                to:drawCards(needDraw, bowangzuo.name)
                room:sendLog{type = "#WangzuoDrawCards", from = player.id, arg = {to.id, needDraw}}
            end
        
        -- 弃牌至当前体力值（与补牌无交集）
        else
            local targetHand = to:getHandcardNum()
            local needDiscard = targetHand - myHp
            
            print("[弃牌逻辑] 当前手牌数：", targetHand, "目标体力：", myHp, "需弃置：", needDiscard)
            if needDiscard > 0 then
                room:askToDiscard(to, {
                    min_num = needDiscard,
                    max_num = needDiscard,
                    include_equip = true,  -- 允许弃置装备
                    skill_name = bowangzuo.name,
                    cancelable = false,
                    prompt = "#bowangzuo-discard:" .. myHp,
                    force = true,  -- 强制弃牌（无手牌时弃装备）
                })
            elseif needDiscard < 0 then
                room:sendLog{type = "#WangzuoNoNeedDiscard", from = player.id, arg = {to.id, myHp}}
            end
        end
    end,
})

return bowangzuo
